package com.zja.servlet;


import com.zja.model.Comment;
import com.zja.model.JsonResponse;
import com.zja.service.CommentService;
import com.zja.service.Impl.CommentServiceImpl;
import com.zja.util.ServletUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;
import java.sql.Timestamp;

/**
 * @author JA
 * @date: 2025/5/5
 * description: 评论接口，支持新增、获取、删除
 */
@WebServlet("/api/comment")
public class CommentServlet extends HttpServlet {
    private final CommentService commentService = new CommentServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 添加评论
        try {
            Comment comment = ServletUtil.readJsonBody(req, Comment.class);

            // 评论内容校验
            if (comment.getContent() == null || comment.getContent().trim().isEmpty()) {
                ServletUtil.writeJsonResponse(resp, JsonResponse.error("评论内容不能为空"));
                return;
            }
            if (comment.getContent().length() > 500) {
                ServletUtil.writeJsonResponse(resp, JsonResponse.error("评论内容不能超过500字"));
                return;
            }

            comment.setCreatedAt(new Timestamp(System.currentTimeMillis()));
            comment.setUpdatedAt(comment.getCreatedAt());

            boolean success = commentService.addComment(comment);
            if (success) {
                ServletUtil.writeJsonResponse(resp, JsonResponse.success("评论成功"));
            } else {
                ServletUtil.writeJsonResponse(resp, JsonResponse.error("评论失败"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            ServletUtil.writeJsonResponse(resp, JsonResponse.error("评论异常: " + e.getMessage()));
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");

        if ("tree".equals(action)) {
            String articleIdStr = req.getParameter("articleId");
            if (articleIdStr == null) {
                ServletUtil.writeJsonResponse(resp, JsonResponse.error("缺少 articleId 参数"));
                return;
            }

            Long articleId;
            try {
                articleId = Long.parseLong(articleIdStr);
            } catch (NumberFormatException e) {
                ServletUtil.writeJsonResponse(resp, JsonResponse.error("articleId 参数格式错误"));
                return;
            }

            List<Comment> commentTree = commentService.getCommentTreeByArticleId(articleId);
            ServletUtil.writeJsonResponse(resp, JsonResponse.success(commentTree));
        } else {
            ServletUtil.writeJsonResponse(resp, JsonResponse.error("无效的 action 参数"));
        }
    }


    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 删除评论
        try {
            Long commentId = Long.parseLong(req.getParameter("commentId"));
            boolean success = commentService.deleteComment(commentId);
            if (success) {
                ServletUtil.writeJsonResponse(resp, JsonResponse.success("删除成功"));
            } else {
                ServletUtil.writeJsonResponse(resp, JsonResponse.error("删除失败"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            ServletUtil.writeJsonResponse(resp, JsonResponse.error("删除异常: " + e.getMessage()));
        }
    }
}
